﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using WeChat.PlatForm.Common;
using WeChat.PlatForm.DataAccess.Repository;
using WeChat.PlatForm.DTO;
using WeChat.PlatForm.Entity;
using WeChat.PlatForm.IService;

namespace WeChat.PlatForm.Service
{
    class MemberService : IMemberService
    {
        MemberRepository _member;

        public MemberService()
        {
            _member = new MemberRepository();
        }

        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="nick"></param>
        /// <returns></returns>
        public Task<int> RegisterMember(string password, string email, string nick)
        {
            return _member.RegisterMember(CommonHelp.MD5Encrypt(password), email,nick);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<int> ToLogin(string email, string password)
        {
            MemberEntity member = await _member.GetMemberByName(email);

            //该用户没有注册或者密码错误。这里不能具体的告诉UI显示用户名错误还是密码错误，那样如果遭到攻击很容易被黑
            if (member == null|| member.PASS_WORD != CommonHelp.MD5Encrypt(password))
            {
                return 0;
            }

            return 1;
        }
    }
}
